package com.yuwei.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yuwei.bean.Meta;
import com.yuwei.bean.Right;
import com.yuwei.service.RightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class RightController {
    @Autowired
    private RightService rightService;

    @GetMapping("/rights/{type}")
    public String getRights(@PathVariable String type){
        Meta retMeta = new Meta();
        retMeta.setStatus(200);
        retMeta.setMsg("获取权限列表成功");
        if("list".equals(type)){
            List<Right> rightList = rightService.getRightList();
            Object retData = new Object() {
                public Meta meta = retMeta;
                public List<Right> data = rightList;
            };
            return JSON.toJSONString(retData);
        }else if("tree".equals(type)){
            Object retData = new Object() {
                public Meta meta = retMeta;
            };
        }
        return "";
    }

//    使用com,alibaba.fastjson包将json字符串转换为Json 对象，在通过get方法取出username值
//    接受java基本类型的post方法写法
    @PostMapping("/test")
    public String postOneString(@RequestBody String userName){
        System.out.println(userName);//json字符串
        JSONObject jsonObject = JSON.parseObject(userName);//解析成json对象
        //通过get方法取值，根据不同的key有不同的get方法，比如getString(),getInteger(),getBytes()等等
        String userName1 = jsonObject.getString("userName");
        System.out.println(userName1);
        Object data = new Object() {
            public String data = "yes";
        };
        return JSON.toJSONString(data);
    }
}
